package com.app.toknow;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.toknow.Activity.AboutActivity;
import com.app.toknow.Activity.LoginActivity;
import com.app.toknow.Activity.ProfileActivity;
import com.app.toknow.Activity.SettingsActivity;
import com.app.toknow.Activity.SlidingApp.SlidingFragmentActivity;
import com.app.toknow.Adapter.PageFragmentAdapter;
import com.app.toknow.Common.SharedPreferencesUtil;
import com.app.toknow.Entity.Channel;
import com.app.toknow.Fragment.HomeOneFragment;
import com.app.toknow.Fragment.LeftMenuFragment;
import com.app.toknow.View.SlidingMenuView.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends SlidingFragmentActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private Context context;
    private SharedPreferences sharedPreferences;
    Button leftButton;
    Button rightButton;
    Button navleftButton;
    RelativeLayout  navLayout;
    TextView titleText;
    private SlidingMenu _SlidingMenu; // 侧边栏菜单
    private LeftMenuFragment _LeftFragment; // 左侧菜单Fragment
    private ListView menuListView;

    private ViewPager viewPager;
    private RadioGroup rgChannel=null;
//    private HorizontalScrollView hvChannel;
    private PageFragmentAdapter adapter=null;
    private List<Fragment> fragmentList=new ArrayList<Fragment>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initNavView();
        initSlidingMenu();
        initContentView();
        initTab();
        initViewPagerData();
    }
    private void initNavView()
    {
        navleftButton = (Button)findViewById(R.id.nav_leftButton);
        navleftButton.setOnClickListener(this);
    }
    private void initContentView(){
        rgChannel=(RadioGroup)super.findViewById(R.id.rgChannel);
        viewPager=(ViewPager)super.findViewById(R.id.vpNewsList);
//        hvChannel=(HorizontalScrollView)super.findViewById(R.id.hvChannel);
        rgChannel.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group,int checkedId) {
                        viewPager.setCurrentItem(checkedId);
                    }
                });
        viewPager.setOnPageChangeListener(this);

    }
    private void initTab(){
        List<Channel> selectedChannel=new ArrayList<Channel>();
        selectedChannel.add(new Channel("","头条",0,"",""));
        selectedChannel.add(new Channel("","娱乐",0,"",""));
        List<Channel> channelList=selectedChannel;
        for(int i=0;i<channelList.size();i++){
            RadioButton rb=(RadioButton)LayoutInflater.from(this).inflate(R.layout.tab_radio, null);
            rb.setId(i);
            rb.setText(channelList.get(i).getName());
            if(i==0)
                 rb.setChecked(true);
            RadioGroup.LayoutParams params=new
                    RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);

            rgChannel.addView(rb,params);
        }

    }
    private void initViewPagerData(){
        List<Channel>   selectedChannel=new ArrayList<Channel>();
        selectedChannel.add(new Channel("","头条",0,"",""));
        selectedChannel.add(new Channel("","娱乐",0,"",""));
        List<Channel> channelList= selectedChannel;
        for(int i=0;i<channelList.size();i++){
            HomeOneFragment frag=new HomeOneFragment();
            Bundle bundle=new Bundle();
            bundle.putString("weburl", channelList.get(i).getWeburl());
            bundle.putString("name", channelList.get(i).getName());
            frag.setArguments(bundle);
            fragmentList.add(frag);
        }
        adapter=new PageFragmentAdapter(super.getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
        //viewPager.setOffscreenPageLimit(0);
    }

    /**
     * 初始化侧边栏菜单
     */
    private void initSlidingMenu() {
        _LeftFragment = new LeftMenuFragment(); // 创建左边菜单Fragment
//        _RightFragment = new RightMenuFragment();// 创建右边菜单Fragment

        _SlidingMenu = getSlidingMenu(); // 由于Activity继承自SlidingFragmentActivity,所以直接获取当前的侧边栏菜单
        _SlidingMenu.setMode(SlidingMenu.LEFT); // 设置侧边栏菜单为左右模式
        _SlidingMenu.setBehindWidthRes(R.dimen.left_menu_width); // 设置左边菜单的宽度,该值为左菜单展开的宽度
        _SlidingMenu.setShadowDrawable(R.drawable.shadow); // 设置左菜单的阴影
        _SlidingMenu.setShadowWidth(15); // 设置阴影宽度
        _SlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); // 设置侧边栏菜单触摸模式

        setBehindContentView(R.layout.fragment_left_menu); // 设置左菜单的默认VIEW
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.main_left_menu, _LeftFragment).commit(); // 将左菜单默认VIEW替换为左菜单Fragment

//        _SlidingMenu.setSecondaryMenu(R.layout.right_menu_layout); // 设置右菜单默认VIEW
//        _SlidingMenu.setSecondaryShadowDrawable(R.drawable.shadowright); // 设置右菜单阴影
//        _SlidingMenu.setRightBehindWidthRes(R.dimen.right_menu_width); // 设置右菜单的宽度,该值为右菜单展开的宽度
//        getSupportFragmentManager().beginTransaction()
//                .replace(R.id.rightMenu, _RightFragment).commit(); // 将右菜单默认VIEW替换为右菜单Fragment

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_leftButton:
                if (!_SlidingMenu.isMenuShowing()) {
                    _SlidingMenu.showMenu();
                } else {
                    _SlidingMenu.showContent();
                }
                break;
            case R.id.itbtn_settings:
//                if (_SlidingMenu.isMenuShowing()) {_SlidingMenu.showContent();}
                Intent intent_settings = new Intent(this, SettingsActivity.class);
                startActivity(intent_settings);
                break;
            case R.id.itbtn_about:
//                if (_SlidingMenu.isMenuShowing()) {_SlidingMenu.showContent();}
                Intent intent_about = new Intent(this, AboutActivity.class);
                startActivity(intent_about);
                break;
            case R.id.itbtn_exit:
                SharedPreferencesUtil.put(this,"isLoginApp",false);
                SharedPreferencesUtil.put(this,"LoginApp_UserID",-2);
                Intent intent_exit = new Intent(this, LoginActivity.class);
                startActivity(intent_exit);
                break;
            case R.id.main_left_ment_headImage:

                Intent intent_profile = new Intent(this, ProfileActivity.class);
                startActivity(intent_profile);
                if (_SlidingMenu.isMenuShowing()) {_SlidingMenu.showContent();}
                break;
//            case R.id.leftButton:
//                if (!_SlidingMenu.isMenuShowing()) {
//                    _SlidingMenu.showMenu();
//                } else {
//                    _SlidingMenu.showContent();
//                }
//                break;
//            case R.id.rightButton:
//                if (!_SlidingMenu.isSecondaryMenuShowing()) {
//                    _SlidingMenu.showSecondaryMenu();
//                } else {
//                    _SlidingMenu.showContent();
//                }
//                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        setTab(position);
    }
    /**
     * 滑动ViewPager时调整ScroollView的位置以便显示按钮
     * @param idx
     */
    private void setTab(int idx){
        RadioButton rb=(RadioButton)rgChannel.getChildAt(idx);
        rb.setChecked(true);
        int left=rb.getLeft();
        int width=rb.getMeasuredWidth();
        DisplayMetrics metrics=new DisplayMetrics();
        super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth=metrics.widthPixels;
        int len=left+width/2-screenWidth/2;
//        hvChannel.smoothScrollTo(len, 0);//滑动ScroollView
    }
    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
